/**
 * 
 */
package com.aceitunaproject.webclient.server;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.aceitunaproject.clientconnectionmanager.ClientConnectionManager;
import com.aceitunaproject.clientconnectionmanager.ClientInterfaceHolder;
import com.aceitunaproject.clientconnectionmanager.DefaultClientInterfaceFactory;
import com.aceitunaproject.clientconnectionmanager.MaximumConnectionLimitExceeded;
import com.aceitunaproject.clientinterface.AceitunaClientInterface;
import com.aceitunaproject.shared.gamelement.Unit;
import com.aceitunaproject.shared.map.MapTile;
import com.aceitunaproject.webclient.client.AceitunaService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * @author Federico Bergstein
 * 
 */
@SuppressWarnings("serial")
public class AceitunaServiceImpl extends RemoteServiceServlet implements
		AceitunaService {

	static int LoginTimeout = 10000;
	static int GetMapTimeout = 10000;
	static ClientConnectionManager connectionManager = new ClientConnectionManager(
			new DefaultClientInterfaceFactory());

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.aceitunaproject.webclient.client.AceitunaService#login(java.lang.
	 * String, java.lang.String)
	 */
	@Override
	public String login(String userName, String password) throws Exception {
		String id = null;
		try {
			id = connectionManager.createConnection().toString();
		} catch (MaximumConnectionLimitExceeded e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ClientInterfaceHolder clientInterfaceHolder = connectionManager
				.getClientInterfaceHolder(UUID.fromString(id));
		AceitunaClientInterface theInterface = clientInterfaceHolder
				.getClientInterface();
		theInterface.setPlayer(userName);
		theInterface.login();

		Date start = new Date();
		while (!clientInterfaceHolder.isLoggedIn()) {
			if (start.getTime() - new Date().getTime() > LoginTimeout) {
				throw new Exception("Not logged in after timeout");
			}
		}

		return id;
	}

	@Override
	public List<MapTile> getMap(String clientId) throws Exception {

		ClientInterfaceHolder clientInterfaceHolder = connectionManager
				.getClientInterfaceHolder(UUID.fromString(clientId));

		connectionManager.getConnection(UUID.fromString(clientId))
				.requestMapRefresh();

		Date start = new Date();
		while (!clientInterfaceHolder.hasReceivedMap()) {
			if (start.getTime() - new Date().getTime() > LoginTimeout) {
				throw new Exception("Not logged in after timeout");
			}
		}

		return connectionManager.getClientInterfaceHolder(
				UUID.fromString(clientId)).getLastMap();
	}

	@Override
	public void MoveUnit(String clientId,long unitID, int x,int y) {
		connectionManager.getConnection(UUID.fromString(clientId)).moveUnit(unitID, x, y);
		
		
	}

	@Override
	public void BuildSettlement(String clientId, Long unitID, String settlementName,int x, int y) {
		connectionManager.getConnection(UUID.fromString(clientId)).buildSettlement(unitID, settlementName, x, y);
		
	}

}
